// 根目录
var webRoot;
/** 获取根目录 */
function initWebRoot(hostUrl) {
	webRoot = hostUrl;
}
/** 通过URL请求JSON数据 */
$(function(){	
	$.extend({
		requestData:function(url,obj){
			$.ajax({
			  url: url,
			  dataType: 'json',
			  context:{param:obj},
			  success: showData
			});
		},
	});
});

/** 判断是否获取到JSON数据 */
var firstFlag = true;
var nowTab = "";
function showData(json){
	if(json == null){
		var nulDoc = new Array();
		nulDoc.push("<center>");
		nulDoc.push("找不到符合类型的信息，请查看");
		nulDoc.push("<a href='javascript:;' id='all_list' onclick='javascript:toIndex();'>全部信息</a>");
		nulDoc.push("<center>");
		this.param.html(nulDoc.join(""));
	}else{
		var total = json.total;
		var pageSize = json.pageSize;
		var allDoc = new Array();
		$.each(json.datas,function(index,multipObj){
			allDoc.push(commodityPaser(multipObj));
		});
		var aim = this.param;
		aim.html(allDoc.join(""));
		var comeTab = this.param2;
		if(firstFlag ||(nowTab!=comeTab)){
			nowTab = comeTab;
			$('#pager').mypagination(total,{
				perPage:pageSize,
				root:webRoot,
				callback:function(page){
					$.requestData(webRoot+"/"+childUrl+"?offset="+page+"& t="+Math.random(),aim,comeTab);
				}
			});
			firstFlag = false;
		}
	}
	
}

/** 商品列表（JSON组装成HTML）*/
function commodityPaser(multipObj){
	
	var commodity = multipObj.commodity;
	var user = multipObj.safetyUser;
	
	var noteDoc = new Array();
	noteDoc.push("<div class='list_item'>");
	noteDoc.push("<div class='item_avatar'><img src='"+webRoot+"/images/avatar_02.jpg' width='64' height='64' alt='avatar_01'/></div>");
	noteDoc.push("<div class='item_content'>");
	noteDoc.push("<div class='item_left'>");
	noteDoc.push("<a href='#' class='item_title'>"+commodity.commodityTitle+"</a>");
	noteDoc.push("<div class='item_info'><ul>");
	noteDoc.push("<li><div>名称&nbsp;:&nbsp; "+commodity.commodityName+"</div>");
	noteDoc.push("<li><div>价格&nbsp;:&nbsp; "+commodity.commodityPrice+"元</div></li>");
	noteDoc.push("<li><div>新旧程度&nbsp;:&nbsp; "+commodity.commodityCondition+"</div></li>");
	noteDoc.push("<li><div>联系方式&nbsp;:&nbsp; "+commodity.contactType+"</div></li>");
	noteDoc.push("<li><div>说明&nbsp;:&nbsp; "+commodity.commodityExplain+"</div></li><br/>");
	noteDoc.push("<li><div>发布日期&nbsp;:&nbsp; "+getGracefullTime(commodity.writeTime)+"</div></li>");
	noteDoc.push("</ul></div></div><div class='item_right'><div class='item_img'>");
	noteDoc.push("<img src='"+webRoot+"/images/book_img_02.png' width='150' height='175' alt='book01' />");
	noteDoc.push("</div><div class='item_more'>");
	noteDoc.push(" <a href='#'><span><img src='"+webRoot+"/images/xiangqing.png' width='15' height='14'/></span>详情</a>");
	noteDoc.push("</div></div></div><div class='item_triangle'></div></div>");
	return noteDoc.join("");
	
}

/** 
 * ------------------- 优化日期格式 --------------------------
 */

function getGracefullTime(  javaDate  ){
	if( javaDate != null ){
		
		var historyMillisecond = Date.parse( javaDate ) ;
		var historyDay = new Date( historyMillisecond ).getDate() ;
		
		var now = new Date();

		var difftime = now.getTime() - historyMillisecond ;

		if( difftime < 10 * 1000 ){
			//0.小于10秒钟用刚刚来表示
			gracefulTime = "刚刚" ;
		}else if( difftime < 60 * 1000 ){
			//1.小于一分钟用秒来表示
			gracefulTime = parseInt((difftime%(60*1000))/1000) + "秒前" ;
		}else if( difftime < 60 * 60 * 1000 ){
			//2.小于一小时用分钟来表示 
			gracefulTime =parseInt( (difftime%(60 * 60 * 1000))/(1000*60) )+ "分钟前" ;
		}else if( difftime < 24* 60 * 60 * 1000 ){
			//4.小于24小时用具体的小时分钟来表示
			if( difftime < 12* 60 * 60 * 1000 ){
				//3.小于12小时用小时来表示
				gracefulTime =parseInt( (difftime%(12* 60 * 60 * 1000))/(1000*60*60) ) + "小时前" ;
			}else{
				var tempDate = new Date();
				tempDate.setTime(historyMillisecond);
				gracefulTime = ( isYesterday( historyDay )?"昨天":"今日 " ) + tempDate.format("hh:mm");
			}
		}else if( difftime < 7*24*60 * 60 * 1000 ){
			//5.小于一周用天来表示
			gracefulTime = parseInt( (difftime%(7*24*60 * 60 * 1000))/(1000*60*60*24) )+ "天前" ;
		}else {
			//6.超过一个星期用具体的时间来表示
			gracefulTime = new Date(historyMillisecond).format("yyyy年MM月dd日 hh:mm");
		}
		return gracefulTime;
	}
	
	return "";
}
/** 仅判断是否为昨天 */
function isYesterday( historyDay ){
	var today = new Date();
	if(  ( today.getDate() - historyDay )== 1){
		return true ;
	}else{
		return false ;
	}
}

Date.prototype.format = function(format) { 
  var o = { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "h+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  }; 
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
};
